//
//  ShareManager.m
//  GainInvest
//
//  Created by 王玉龙 on 17/3/24.
//  Copyright © 2017年 longlong. All rights reserved.
//

#define BUFFER_SIZE 1024 * 100


#import "ShareManager.h"
#import "WXApi.h"
@implementation ShareManager

+ (void)weChatShareDetaileString:(NSString *)string ViewController:(UIViewController *)viewController
{
    if ([AuthorizationManager isBindingMobile] == NO)
    {
        [AuthorizationManager getBindingMobileWithViewController:viewController IsNeedCancelClick:NO];
        return;
    }
    else if ([WXApi isWXAppInstalled] == NO)
    {
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"本地没有微信" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action){}];
        [actionSheet addAction:cancelAction];
        [viewController presentViewController:actionSheet animated:YES completion:nil];
        return;
        
    }
    else if ([WXApi isWXAppSupportApi] == NO)
    {
        UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"不支持微信" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action){}];
        [actionSheet addAction:cancelAction];
        [viewController presentViewController:actionSheet animated:YES completion:nil];
        return;
    }
    
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = @"首单免费，安全正规的小额投资平台，8元起投，操作简单，盈利可立即提现";
    [message setThumbImage:[UIImage imageNamed:@"owner_AppLogo"]];
    
    WXAppExtendObject *ext = [WXAppExtendObject object];
    ext.extInfo = @"<xml>extend info</xml>";
    ext.url = ShareUrl;
    Byte* pBuffer = (Byte *)malloc(BUFFER_SIZE);
    memset(pBuffer, 0, BUFFER_SIZE);
    NSData* data = [NSData dataWithBytes:pBuffer length:BUFFER_SIZE];
    free(pBuffer);
    
    ext.fileData = data;
    
    message.mediaObject = ext;
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneTimeline;
    
    [WXApi sendReq:req];

}

+ (void)umShareClick:(UMSocialPlatformType)platformType DetaileString:(NSString *)string ViewController:(UIViewController *)viewController
{
    
    if ([[UMSocialManager defaultManager] isInstall:platformType] == NO)
    {
        NSString *platName = @"";
        
        switch (platformType)
        {
            case UMSocialPlatformType_WechatSession:
            case UMSocialPlatformType_WechatTimeLine:
                platName = @"微信";
                break;
            case UMSocialPlatformType_QQ:
            case UMSocialPlatformType_Qzone:
                platName = @"QQ";
                break;
            default:
                break;
        }
        
        NSString *string = [NSString stringWithFormat:@"您的手机没有安装%@",platName];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"分享失败" message:string preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action){}];
        [alertController addAction:cancelAction];
        [viewController presentViewController:alertController animated:YES completion:nil];
        
        return;
    }
    

    
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    
    UMShareWebpageObject *shareURLObject = [UMShareWebpageObject shareObjectWithTitle:string descr:nil thumImage:[UIImage imageNamed:@"owner_AppLogo"]];
    shareURLObject.webpageUrl = ShareUrl;
    messageObject.shareObject = shareURLObject;
    
    
    
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:viewController completion:^(id data, NSError *error)
     {
         if (error)
         {
             NSLog(@"************Share fail with error %@*********",error);
         }
         else
         {
             NSLog(@"response data is %@",data);
         }
     }];
}


@end
